package com.msmall.community.baby.preparer;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.ViewPreparer;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.Constants;
import com.msmall.community.ApplicationManager;
import com.msmall.community.bbs.model.Board;
import com.msmall.community.bbs.model.Topic;
import com.msmall.community.bbs.service.TopicManager;
import com.msmall.community.common.CascadeCollection;
import com.msmall.community.common.model.Commend;

public class BabyIndexBBSPreparer implements ViewPreparer{
	
	private static final Log log = LogFactory.getLog(BabyIndexBBSPreparer.class);
	
	public void execute(TilesRequestContext tilesContext, AttributeContext context) {
		try{
			HttpServletRequest request = (HttpServletRequest) tilesContext.getRequest();
	
			List<Commend> topics = (List<Commend>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_BBS_COMMEND_IMAGE_LIST);
			if(topics != null && topics.size() > 0){
				request.setAttribute("babyIndexBBSImage", topics.get(0));
			}
			List<Topic> list = new ArrayList<Topic>();//在首页整体显示30条记录
			List<Topic> topnewscommendlist = (List<Topic>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_BBS_COMMEND_TOPNEWS_LIST);
			List<Topic> topnewsbelowcommendlist = (List<Topic>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_BBS_COMMEND_TOPNEWS_BELOW_LIST);
			List<Topic> hottopiccommendlist = (List<Topic>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_BBS_COMMEND_HOTTOPIC_LIST);
			TopicManager tm = (TopicManager) ApplicationManager.appContext.getBean("topicManager");
			List<Topic> hottodaytopiclist = tm.hotTopic(30);
			
			list.addAll(topnewscommendlist);
			list.addAll(topnewsbelowcommendlist.subList(0, topnewsbelowcommendlist.size()>=5?5:topnewsbelowcommendlist.size()));
			list.addAll(hottopiccommendlist.subList(0, topnewsbelowcommendlist.size()>=5?5:topnewsbelowcommendlist.size()));
			int suffix = 30 - list.size();
			list.addAll(hottodaytopiclist.subList(0, hottodaytopiclist.size()>=suffix?suffix:hottodaytopiclist.size()));
			request.setAttribute("babyIndexBBSList", list);
			
			CascadeCollection<Board> cs = (CascadeCollection<Board>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_BBS_BOARD_CASCADECOLLECTION);
			request.setAttribute("babyIndexBBSBoards", cs);
			
			List<Commend> babyIndexBBSTopNews = (List<Commend>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_INDEX_COMMEND_BBS_TOPNEWS);
			request.setAttribute("babyIndexBBSTopNews", babyIndexBBSTopNews);
		
			List<Commend> babyIndexBBSContentImage = (List<Commend>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_INDEX_COMMEND_BBS_CONTENT_IMAGE);
			request.setAttribute("babyIndexBBSContentImage", babyIndexBBSContentImage!=null&&babyIndexBBSContentImage.size()>0?babyIndexBBSContentImage.get(0):null);
		}catch(Exception e){
			log.debug(e);
		}	
	}
}

